clear

**This is our final sample; not bootstrap samples drop observations with no lags: we need to recreate lags, so go from here
use "$BS_fold/local1/decomposition_SS_ProdR_new_kl.dta"
keep co_code1 year
tempfile t1
save `t1', replace

use "$working/prowess_wits_estimation_demand", clear
merge m:1 year co_code1 using `t1'
keep if _merge == 3
drop _merge

gen p = ln(sales_value / sales_quantity)
gen q = ln(sales_quantity)
drop if missing(sales_quantity)

sort co_code1 product_name5 sales_unit year
egen fpu = group(co_code1 product_name5 sales_unit)
xtset  fpu year, yearly
destring(nic_08_2dig), g(nic2)

foreach v in r p q ln_IND ln_IVS {
	gen diff_`v' = `v' - L.`v'
}

foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}


global n = 2
global rhs1 diff_ln_IND 
global rhs2 diff_wIPT_IND_CHN
global ivs1 diff_ln_IVS 
global ivs2 diff_wIPT_LMI_CHN_EXCL_IND
global ctrl 

ivreghdfe diff_p (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND ) , a(year) first cluster(nic_08_4dig)
	eststo price_1
	local N_1= e(N)
	mat f = e(first)
	local F_OUT_1 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_1 = round(f[8,2], .01)
	
ivreghdfe diff_p (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND )  , a(year nic2) first cluster(nic_08_4dig)
	eststo price_2
	local N_2= e(N)
	mat f = e(first)
	local F_OUT_2 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_2 = round(f[8,2], .01)
	

		
ivreghdfe diff_p (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND ) , a(nic2#year) first cluster(nic_08_4dig)
	eststo price_3
	local N_3= e(N)
	mat f = e(first)
	local F_OUT_3 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_3 = round(f[8,2], .01)

ivreghdfe diff_p (diff_ln_IND  = diff_ln_IVS ) diff_wIPT_LMI_CHN_EXCL_IND, a(year) first cluster(nic_08_4dig)
	eststo price_4
	local N_4= e(N)
	mat f = e(first)
	local F_OUT_4 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_4 = round(f[8,2], .01)
				

ivreghdfe diff_p (diff_ln_IND   = diff_ln_IVS)  diff_wIPT_LMI_CHN_EXCL_IND, a(year nic2) first cluster(nic_08_4dig)
	eststo price_5
	local N_5= e(N)
	mat f = e(first)
	local F_OUT_5 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_5 = round(f[8,2], .01)

		
ivreghdfe diff_p (diff_ln_IND  = diff_ln_IVS) diff_wIPT_LMI_CHN_EXCL_IND , a(nic2#year) first cluster(nic_08_4dig)
	eststo price_6
	local N_6= e(N)
	mat f = e(first)
	local F_OUT_6 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_6 = round(f[8,2], .01)
	
label var diff_ln_IND "OutputComp"
label var diff_wIPT_IND_CHN "InputAccess"
label var diff_wIPT_LMI_CHN_EXCL_IND "InputAccessIV"
	

esttab price_1 price_2 price_3 price_4 price_5 price_6 using "$tables/P_Regs.tex" , cells(b(nostar fmt(%9.3f)) se(par fmt(%9.3f))) nodepvars ///
eqlabels(none) nomtitle keep(diff_ln_IND diff_wIPT_IND_CHN diff_wIPT_LMI_CHN_EXCL_IND) label ///
 collabels(none) replace 	prefoot("\hline" "F-stat OutputComp & `F_OUT_1' & `F_OUT_2' & `F_OUT_3' & `F_OUT_4' & `F_OUT_5' & `F_OUT_6' \\" ///
	"F-Stat InputAccess & `F_IN_1' & `F_IN_2' &  `F_IN_3' &  `F_IN_4' &  `F_IN_5' &  `F_IN_6'  \\" )

	

ivreghdfe diff_q (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND ) , a(year) first cluster(nic_08_4dig)
	eststo q_1
	local N_1= e(N)
	mat f = e(first)
	local F_OUT_1 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_1 = round(f[8,2], .01)
	
ivreghdfe diff_q (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND )  , a(year nic2) first cluster(nic_08_4dig)
	eststo q_2
	local N_2= e(N)
	mat f = e(first)
	local F_OUT_2 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_2 = round(f[8,2], .01)
	

		
ivreghdfe diff_q (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND ) , a(nic2#year) first cluster(nic_08_4dig)
	eststo q_3
	local N_3= e(N)
	mat f = e(first)
	local F_OUT_3 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_3 = round(f[8,2], .01)

ivreghdfe diff_q (diff_ln_IND  = diff_ln_IVS ) diff_wIPT_LMI_CHN_EXCL_IND, a(year) first cluster(nic_08_4dig)
	eststo q_4
	local N_4= e(N)
	mat f = e(first)
	local F_OUT_4 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_4 = round(f[8,2], .01)
				

ivreghdfe diff_q (diff_ln_IND   = diff_ln_IVS)  diff_wIPT_LMI_CHN_EXCL_IND, a(year nic2) first cluster(nic_08_4dig)
	eststo q_5
	local N_5= e(N)
	mat f = e(first)
	local F_OUT_5 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_5 = round(f[8,2], .01)

		
ivreghdfe diff_q (diff_ln_IND  = diff_ln_IVS) diff_wIPT_LMI_CHN_EXCL_IND , a(nic2#year) first cluster(nic_08_4dig)
	eststo q_6
	local N_6= e(N)
	mat f = e(first)
	local F_OUT_6 = round(f[8,1], .01)
	*mat list f
	cap local F_IN_6 = round(f[8,2], .01)
	
label var diff_ln_IND "OutputComp"
label var diff_wIPT_IND_CHN "InputAccess"
label var diff_wIPT_LMI_CHN_EXCL_IND "InputAccessIV"
	

esttab q_1 q_2 q_3 q_4 q_5 q_6 using "$tables/Q_Regs.tex" , cells(b(nostar fmt(%9.3f)) se(par fmt(%9.3f))) nodepvars ///
eqlabels(none) nomtitle keep(diff_ln_IND diff_wIPT_IND_CHN diff_wIPT_LMI_CHN_EXCL_IND) label ///
 collabels(none) replace 	prefoot("\hline" "F-stat OutputComp & `F_OUT_1' & `F_OUT_2' & `F_OUT_3' & `F_OUT_4' & `F_OUT_5' & `F_OUT_6' \\" ///
	"F-Stat InputAccess & `F_IN_1' & `F_IN_2' &  `F_IN_3' &  `F_IN_4' &  `F_IN_5' &  `F_IN_6'  \\" )
